package com.zbkj.front.controller;

import com.zbkj.dal.pojo.request.CreateDuesOrderRequest;
import com.zbkj.dal.pojo.request.PaySheOrderRequest;
import com.zbkj.dal.pojo.response.CreateDuesOrderResponse;
import com.zbkj.dal.pojo.response.DuesOrderDetailVO;
import com.zbkj.dal.pojo.response.OrderPayResultResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.DuesOrderService;
import com.zbkj.service.service.DuesPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用于除了“买卖订单“之外的订单的线上交易接口
 */
@Slf4j
@RestController
@RequestMapping("api/front/she/trade")
@Api(tags = "线上交易")
public class SheTradeController {

    @Autowired
    private DuesPayService duesPayService;

    @Autowired
    private DuesOrderService duesOrderService;

    @ApiOperation(value = "创建会费充值订单")
    @RequestMapping(value = "/createDuesOrder", method = RequestMethod.POST)
    public CommonResult<CreateDuesOrderResponse> createDuesOrder(@RequestBody @Validated CreateDuesOrderRequest createDuesOrderRequest) {
        return CommonResult.success(duesOrderService.createDuesOrder(createDuesOrderRequest));
    }

    @ApiOperation(value = "支付会费充值订单", tags = {"认购订单", "会员权益"})
    @RequestMapping(value = "/paymentDuesOrder", method = RequestMethod.POST)
    public CommonResult<OrderPayResultResponse> paymentDuesOrder(@RequestBody @Validated PaySheOrderRequest paySheOrderRequest) {
        return CommonResult.success(duesPayService.payment(paySheOrderRequest));
    }

    @ApiOperation(value = "取消会费充值订单")
    @PostMapping(value = "/cancel/dues-order")
    public CommonResult<Void> duesOrderCancel(@RequestParam("orderNo") String orderNo) {
        duesPayService.duesOrderCancel(orderNo);
        return CommonResult.success();
    }

    @ApiOperation(value = "充值单详情")
    @GetMapping("/dues-order/detail/{orderNo}")
    public CommonResult<DuesOrderDetailVO> duesOrderDetail(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(duesOrderService.duesOrderDetail(orderNo));
    }

    /**
     * 商户在没有接收到微信支付结果通知的情况下需要主动调用查询订单API查询支付结果。
     * https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/query-by-wx-trade-no.html
     *
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "查询会费充值订单微信支付结果")
    @RequestMapping(value = "/query/wechat/pay/result/{orderNo}", method = RequestMethod.GET)
    public CommonResult<Boolean> queryPayResult(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(duesPayService.queryWechatPayResult(orderNo));
    }
}
